package com.twu.biblioteca;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.event.*;
import java.util.ArrayList;


/**
 * Created with IntelliJ IDEA.
 * User: Deepika
 * Date: 5/13/13
 * Time: 6:53 PM
 * To change this template use File | Settings | File Templates.
 */

import static java.awt.Component.BOTTOM_ALIGNMENT;
import static javax.swing.JOptionPane.showMessageDialog;

class BOOK {
    private String name;
    private int available;            // 1 indicates book is available, 0 indicates not available

    BOOK(String name, int available){
        this.name = name;
        this.available = available;
    }

    public String get_name(){
        return name;
    }

    public int check_availability(){
        return available;
    }

    public void set_availability(int available){
        this.available = available;
    }
}



public class Biblioteca{
    private JPanel panel1;
    private JToolBar ToolBar;
    private JButton viewBooksInLibraryButton;
    private JButton reserveABookButton;

    private JButton reserveButton;
    private JButton membershipDetailsButton;
    private ArrayList<BOOK> books = new ArrayList<BOOK>();
    JList list1;


    private void initialise_books() {
        BOOK b1 = new BOOK("Gone with the Wind",1);
        BOOK b2 = new BOOK("Sherlock Homes",0);
        books.add(b1);
        books.add(b2);
    }


    public Biblioteca() {

        //do not display the reserve button initially
        reserveButton.setVisible(false);
        list1.setVisible(false);
        //String[] data = {"one", "two", "three", "four"};
        //list1 = new JList(data);
        //add contents to jlist
        //DefaultListModel listModel = new DefaultListModel();
        //list1 = new JList(listModel);


        initialise_books();

        for(BOOK b: books){
            //showMessageDialog(null, b.get_name());
            //listModel.addElement(b.get_name());
        }


            viewBooksInLibraryButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                list1.setVisible(true);
                list1.setEnabled(false);
                reserveButton.setVisible(false);
            }
        });


        reserveABookButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                list1.setVisible(true);
                list1.setEnabled(true);
                reserveButton.setVisible(true);
            }
        });


        membershipDetailsButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showMessageDialog(null, "Please talk to a Librarian. Thank you.");
            }
        });

        reserveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int index = list1.getSelectedIndex();
                showMessageDialog(null, list1.getSelectedValue());
                BOOK b = books.get(index);
                if(b.check_availability() == 0){
                    showMessageDialog(null, "Sorry we don't have that book yet.");
                }
                else  {
                    showMessageDialog(null, "Thank You! Enjoy the book.");
                    b.set_availability(0);
                }
            }
        });


    }




    public static void main(String[] args) {
        JFrame frame = new JFrame("Welcome to Biblioteca");
        frame.setSize(500,500);
        frame.setLocation(300,300);
        frame.setContentPane(new Biblioteca().panel1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.pack();
        frame.setVisible(true);
        showMessageDialog(null, "Welcome to Biblioteca!");
    }


}
